function [ PercentageRatio, ClassificationOutput ] = SvmPC( DataSet, Gamma )
%SVMONETOONE Summary of this function goes here
%   Detailed explanation goes here

[Samples, Labels, Classes] = LoadTrainData(DataSet);

ClassFilter = cell(1, Classes);

SamplesSize = size(Samples);
SamplesCount = SamplesSize(2);

for i = 1:1:Classes
    SelectedSamples = Labels(1,:) == i;
    ClassFilter{i} = Samples(:, SelectedSamples);
end

AlphaY = cell(Classes, Classes);
SVs = cell(Classes, Classes);
Bias = cell(Classes, Classes);
Parameters = cell(Classes, Classes);
nSV = cell(Classes, Classes);
nLabel = cell(Classes, Classes);

for i = 1:1:Classes-1
    iClass = ClassFilter{i};
    iLabels = ones(1, size(iClass, 2));
    
    for j = i+1:1:Classes
        jClass = ClassFilter{j};
        jLabels = zeros(1, size(jClass, 2));
        
        MergedClasses = [iClass jClass];
        MergedLabels = [iLabels jLabels];
        
        [AlphaY{i,j}, SVs{i,j}, Bias{i,j}, Parameters{i,j}, nSV{i,j}, nLabel{i,j}] = RbfSVC(MergedClasses, MergedLabels, Gamma);
    end
end

[Samples, Labels] = LoadTestData(DataSet);
SamplesCount = size(Samples, 2);

PredictedLabels = cell(Classes, Classes);
DecisionValues = cell(Classes, Classes);

for i = 1:1:Classes-1
    for j = i+1:1:Classes
        [PredictedLabels{i,j}, DecisionValues{i,j}] = SVMClass(Samples, AlphaY{i,j}, SVs{i,j}, Bias{i,j}, Parameters{i,j}, nSV{i,j}, nLabel{i,j});
    end
end

ClassificationOutput = zeros(1, SamplesCount);
TotalHitCount = 0;

Probabilities = cell(Classes,Classes);

for i = 1:1:Classes-1
    for j = i+1:1:Classes
        Coefficient = (PredictedLabels{i,j}(1) == 1 & DecisionValues{i,j}(1) > 0) | (PredictedLabels{i,j}(1) == 0 & DecisionValues{i,j}(1) < 0);
        Coefficient = Coefficient * 2 - 1;
        Probabilities{i,j} = DecisionValues{i,j}*Coefficient;
        Probabilities{i,j} = atan(10*Probabilities{i,j})/pi + 0.5;
        Probabilities{j,i} = 1 - Probabilities{i,j};
    end
end

ResultProbabilities = ones(SamplesCount, Classes)/Classes;

for t = 1:1:10
    for s = 1:1:SamplesCount
        MiFactor = zeros(Classes);
        NormalSum = 0;
        
        for i = 1:1:Classes
            for j = 1:1:Classes
                if j~=i
                    MiFactor(i,j) = ResultProbabilities(s, i)/(ResultProbabilities(s, i) + ResultProbabilities(s, j));
                end
            end
        end
        
        for i = 1:1:Classes
            UpperSum = 0;
            LowerSum = 0;
            
            for j = 1:1:Classes
                if j~=i
                    UpperSum = UpperSum + (size(ClassFilter{i}, 2) + size(ClassFilter{j}, 2)) * Probabilities{i,j}(s);
                    LowerSum = LowerSum + (size(ClassFilter{i}, 2) + size(ClassFilter{j}, 2)) * MiFactor(i,j);
                end
            end
            
            ResultProbabilities(s, i) = ResultProbabilities(s, i) * (UpperSum / LowerSum);
            NormalSum = NormalSum + ResultProbabilities(s,i);
        end
        
        ResultProbabilities(s, :) = ResultProbabilities(s, :) / NormalSum;
    end
end

for s = 1:1:SamplesCount
    [C, I] = max(ResultProbabilities(s,:));
    ClassificationOutput(s) = I;
    
    if I == Labels(s)
        TotalHitCount = TotalHitCount + 1;
    end
end

PercentageRatio = (TotalHitCount/SamplesCount)*100;

end

